<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目详情 - 我的作品集</title>
    
    <link rel="stylesheet" href="css/global.css">
    <link rel="stylesheet" href="css/navbar.css">
    <link rel="stylesheet" href="css/work-detail.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

</head>
<body>
    <nav class="navbar">
        <div class="logo">
            <a href="index.html">PORTFOLIO</a>
        </div>
        <div class="nav-right">
            <a href="projects.html" class="projects-link">项目列表</a>
            <button class="menu-button" onclick="toggleMenu()">
                <span class="menu-line"></span>
                <span class="menu-line"></span>
                <span class="menu-line"></span>
            </button>
        </div>
    </nav>

    <div id="work-detail" class="work-detail">
    <!-- <div id="project-detail" class="work-detail"> -->
        <!-- 项目详情将通过JavaScript动态生成 -->
    </div>

    <div class="work-navigation">
        <div id="prev-project" class="work-nav-item">
            <!-- 上一个项目将通过JavaScript动态生成 -->
        </div>
        <div id="next-project" class="work-nav-item">
            <!-- 下一个项目将通过JavaScript动态生成 -->
        </div>
    </div>

    <!-- 添加 jQuery 和 lightbox JS -->

    <script src="js/projectsData.js"></script>
    <script>
        function getProjectById(id) {
            return projects.find(p => p.id === id);
        }

        function getProjectIndex(id) {
            return projects.findIndex(p => p.id === id);
        }

        function createProjectDetail() {
            const urlParams = new URLSearchParams(window.location.search);
            const projectId = parseInt(urlParams.get('id'));
            const project = getProjectById(projectId);
            
            if (!project) {
                // 如果项目未找到，跳转到第一个项目
                const firstProject = projects[0];
                if (firstProject) {
                    window.location.href = `project-detail.html?id=${firstProject.id}`;
                    return;
                }
            }

            // 更新页面标题
            document.title = `${project.title} - 项目详情`;

            // 创建项目详情内容
            const detailContainer = document.getElementById('work-detail');
            detailContainer.innerHTML = `
                <div class="work-header">
                    <h1 class="work-title">${project.title}</h1>
                    <div class="work-tags">
                        ${project.tags.map(tag => `<span class="work-tag">${tag}</span>`).join('')}
                    </div>
                    <div class="work-description">
                        <p>${project.description}</p>
                    </div>
                </div>
                <div class="work-gallery">
                    ${project.detailImages.map((img, index) => `
                        <img 
                            src="./${img}" 
                            alt="${project.title} - 图片 ${index + 1}"
                            loading="lazy"
                            onload="this.classList.add('loaded')"
                        >
                    `).join('')}
                </div>
            `;

            // 创建导航链接
            const currentIndex = getProjectIndex(projectId);
            const prevProject = projects[currentIndex - 1];
            const nextProject = projects[currentIndex + 1];

            const prevContainer = document.getElementById('prev-project');
            const nextContainer = document.getElementById('next-project');

            if (prevProject) {
                prevContainer.innerHTML = `
                    <a href="project-detail.html?id=${prevProject.id}" class="work-nav-content">
                        <div class="work-nav-preview" style="background-image: url('./${prevProject.backgroundImage}')"></div>
                        <div class="work-nav-info">
                            <span class="work-nav-label"><i class="fas fa-arrow-left"></i> 上一个项目</span>
                            <h3 class="work-nav-title">${prevProject.title}</h3>
                        </div>
                    </a>
                `;
            } else {
                prevContainer.style.display = 'none';
            }

            if (nextProject) {
                nextContainer.innerHTML = `
                    <a href="project-detail.html?id=${nextProject.id}" class="work-nav-content">
                        <div class="work-nav-preview" style="background-image: url('./${nextProject.backgroundImage}')"></div>
                        <div class="work-nav-info">
                            <span class="work-nav-label">下一个项目 <i class="fas fa-arrow-right"></i></span>
                            <h3 class="work-nav-title">${nextProject.title}</h3>
                        </div>
                    </a>
                `;
            } else {
                nextContainer.style.display = 'none';
            }
        }

        document.addEventListener('DOMContentLoaded', createProjectDetail);
    </script>
    <script src="js/main.js"></script>
</body>
</html>